package com.wxd.framework.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.wxd.framework.common.FrameConstants;
import com.wxd.framework.common.ServerInfo;
import com.wxd.framework.service.FrameService;
import com.wxd.framework.vo.KeyValue;

@Controller
@RequestMapping("/")
public class CommonController extends BaseController {
	
	@Autowired
	private FrameService frameService;
	
	//-------------------------------------------------------excel上传
	/**
	 * 文件数据上传至服务器
	 * @param file
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/templateFileUpload.do")
	public ModelAndView templateFileUpload(@RequestParam("file_localFile") MultipartFile file,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView view = new ModelAndView();
		try {
			//获取服务器上传路径 待理解
			String serverPath = ServerInfo.getServerPath(request);
			File pathFile = new File(serverPath);
			if (!pathFile.exists()) {
				pathFile.mkdirs();
			}
			// 获取上传的文件名称
			String filename = file.getOriginalFilename();
			// 获取上传文件类型的扩展名，先得到.的位置，再截取从.的下一个位置到文件的最后，最后得到扩展名
			String ext = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
			ext = ext.toLowerCase();
			if (ext.equals("xls") || ext.equals("xlsx")) {
				// 上传后保存的文件名称
				String name = System.currentTimeMillis() + "." + ext;
				String serverFileName = serverPath + "/" + name;
				// 文件上传 待确认
				file.transferTo(new File(serverFileName));
				view.addObject("newFileName", name);
				view.setViewName("redirect:setFileUploadFinshedFlag.do");
			} else {
				view.addObject("msg", "格式不正确");
				view.setViewName("redirect:setFileUploadFinshedFlag.do");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return view;
	}
	
	/**
	 * 文件上传完毕后，跳转新页面，设置文件上传结束标识
	 * 
	 * @param newFileName
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/setFileUploadFinshedFlag.do")
	public String setFileUploadFinshedFlag(String newFileName, HttpServletRequest request, ModelMap model) {
		model.addAttribute("newFileName", newFileName);
		return "shared/setFileUploadFinshedFlag";
	}
	
	/**
	 * 跳转到指定的数据展示页面
	 * @param newFileName
	 * @param targetPath
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("/toUploadExcelDataShow.do")
	public String toUploadExcelDataShow(String newFileName,String targetPath, HttpServletRequest request, HttpServletResponse response, ModelMap model){
		model.addAttribute("newFileName", newFileName);
		return targetPath;
	}
	
	//-------------------------------------------------------combobox
	/**
	 * 获取是否停用状态（含空元素）
	 * @param response
	 */
	@RequestMapping(value = "/getIsStopedStateWithEmpty.do")
	public void getIsStopedStateWithEmpty(HttpServletResponse response){
		KeyValue vo = new KeyValue();
		List<KeyValue> list = frameService.getListFromXml("StopedState.xml",vo);
		vo = new KeyValue(FrameConstants.COMBBOX_TEMP_VALUE, FrameConstants.COMBBOX_TEMP_TEXT);
		super.renderListWithEmpty(list, vo, response);
	}
}
